package com.huanletao.reflect;

import org.apache.commons.lang3.reflect.FieldUtils;
import org.apache.commons.lang3.reflect.MethodUtils;
import org.junit.Test;

/**
 * <p>
 *   反射相关api 示例:
 *
 * </p>
 *
 * @package: com.huanletao.api
 * @description:
 * @className: ReflectUtilsApiTest
 * @author: @踏雪无痕...
 * @date: Created in 2022/5/30
 * @copyright: Copyright (c) 2022/5/30
 * @version: V1.0
 */
public class ReflectUtilsApiTest {
    private static String sAbc = "111";
    private String abc = "123";

    @Test
    public void test() throws Exception {
        ReflectUtilsApiTest reflectUtilsApiTest = new ReflectUtilsApiTest();
        // 反射获取对象属性的值
        String value2 = (String) FieldUtils.readField(reflectUtilsApiTest, "abc", true);//123
        // 反射获取类静态属性的值
        String value3 = (String) FieldUtils.readStaticField(ReflectUtilsApiTest.class, "sAbc", true);//111
        // 反射设置对象属性值
        FieldUtils.writeField(reflectUtilsApiTest, "abc", "newValue", true);
        // 反射设置类静态属性的值
        FieldUtils.writeStaticField(ReflectUtilsApiTest.class, "sAbc", "newStaticValue", true);

        //反射调用方法;
        MethodUtils.invokeMethod(new Object(),"");


        // commons写法
        MethodUtils.invokeExactMethod(reflectUtilsApiTest, "testMethod", "testParam");

        //获取注解/;
        //Method[] methods = MethodUtils.getMethodsWithAnnotation(ReflectDemo.class, Test.class);

        // ---------- 类似方法 ----------
        // 调用static方法
        MethodUtils.invokeExactStaticMethod(ReflectUtilsApiTest.class, "testStaticMethod", "testParam");
        // 调用方法(含继承过来的方法)
        MethodUtils.invokeMethod(reflectUtilsApiTest, "testMethod", "testParam");
        // 调用static方法(当前不存在则向父类寻找匹配的静态方法)
        MethodUtils.invokeStaticMethod(ReflectUtilsApiTest.class, "testStaticMethod", "testParam");

        Class<?> aClass = Class.forName("");

    }
}